<?php
/**
 * Created by PhpStorm.
 * User: Sixstar-Peter
 * Date: 2019/4/16
 * Time: 21:46
 */

class Test
{
    static $test = [];
}

$server = new  Swoole\Http\Server('0.0.0.0', 9800);
$server->set(
    ['worker_num' => 1]
);

include "Context.php";
//一个协程的数据被另外一个协程修改了
$server->on('request', function ($req, $response) {

    $key = $req->get['key'];
    //Test::$test=$key; //获取到key就赋值到静态变量
    //var_dump($key);
    Context::put('name', $key);
    if ($key == 'sleep') {
        Co::sleep(10); //模拟耗时操作,挂起了当前协程
    }

    defer(function () {
        Context::delete('name'); // delete
        var_dump("销毁");
    });
    $response->end('协程id为' . Co::getcid() . '----' . Context::get('name'));
});
$server->start();